#include <stdio.h>
#include <math.h>
#include <memory.h>

int maximalRectangle(char **matrix, int matrixSize, int *matrixColSize) {
    int m = matrixSize;
    if (m == 0) {
        return 0;
    }
    int n = matrixColSize[0];
    int left[m][n];
    memset(left, 0, sizeof(left));
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (matrix[i][j] == '1') {
                left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
            }
        }
    }
    int ret = 0;
    for (int j = 0; j < n; ++j) {
        int up[m], down[m];
        memset(up, 0, sizeof(up));
        memset(down, 0, sizeof(down));
        int stk[m], top = 0;
        for (int i = 0; i < m; ++i) {
            while (top > 0 && left[stk[top - 1]][j] >= left[i][j]) {
                top--;
            }
            up[i] = top == 0 ? -1 : stk[top - 1];
            stk[top++] = i;
        }
        top = 0;
        for (int i = m - 1; i >= 0; i--) {
            while (top > 0 && left[stk[top - 1]][j] >= left[i][j]) {
                top--;
            }
            down[i] = top == 0 ? m : stk[top - 1];
            stk[top++] = i;
        }
        for (int i = 0; i < m; ++i) {
            int height = down[i] - up[i] - 1;
            int area = height * left[i][j];
            ret = fmax(ret, area);
        }

    }
    return ret;
}

int main() {
//    [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
    char test[][5] = {{"10100"},
                      {"10111"},
                      {"11111"},
                      {"10010"}};
    char *p[4];
    for (int i = 0; i < 4; ++i) {
        p[i] = test[i];
    }

    int matrixColSize = 5;
    int ret = maximalRectangle(p, 4, &matrixColSize);
    printf(" %d", ret);

}